package com.hanhy.Page;

import com.hanhy.util.ConfigUtil;
import com.hanhy.util.Locator;
import com.hanhy.util.Log;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.HashMap;

/**
 * Created by HuiyingHan on 2016/12/12.
 * 基类：BasePage
 * 实现功能：
 * 1、定义driver
 * 2、实现页面的最大化
 * 3、实现基本的打开页面
 */
public class BasePage {
    public WebDriver driver = null;
    public String url;
    public HashMap<String,Locator> locatorMap;
    protected Log log = new Log(this.getClass());
    public BasePage(WebDriver driver) throws Exception {
        this.driver = driver;
        log.debug(this.getClass().getCanonicalName());
        log.info(System.getProperty("user.dir"));
        locatorMap = ConfigUtil.readXmlUtil(this.getClass().getSimpleName());
    }


    public BasePage() {

    }

    public void open(String url){
        this.driver.get(url);
    }

    public String currentUrl(){
        return this.driver.getCurrentUrl();
    }

    public void type(String locatorName,String value) throws NoSuchElementException{
        log.info("type into the value");
        WebElement e = findElement(this.driver,locatorName);
        if (e!=null){
            log.info("type value is:"+value);
            e.sendKeys(value);
        }else
            log.info("元素为空");
        throw new NoSuchElementException("没有找到相应的元素！");
    }

    public void click(String locatorName) throws NoSuchElementException{
        log.info("click 操作");
        WebElement e = findElement(driver,locatorName);
        if (e!=null){
            log.info("click the button");
            e.click();
        }
        throw new NoSuchElementException("没有找到相应的元素！");
    }

    public  WebElement findElement(WebDriver driver,final String locatorName){
        log.info(this.getClass().getCanonicalName()+".findElement()");
        WebElement element = new WebDriverWait(driver,getLocator(locatorName).getWaitSec()).until(new ExpectedCondition<WebElement>(){
            @Override
            public WebElement apply(WebDriver driver) {
                 return getElement(driver,locatorName);
            }
        } );
        return element;
    }

    public WebElement getElement(WebDriver driver,String locatorName){
        Locator locator = getLocator(locatorName);
        log.info(this.getClass().getCanonicalName()+".getElement()");
        log.info("locator.getByType()的值为"+locator.getByType().toString());
        WebElement e ;
        switch (locator.getByType()){
            case xpath:
                log.debug("find element by xpath");
                e = driver.findElement(By.xpath(locator.getExpression()));
                break;
            case id:
                log.debug("find element by id");
                e = driver.findElement(By.id(locator.getExpression()));
                break;
            case name:
                log.debug("find element by name");
                e = driver.findElement(By.name(locator.getExpression()));
                break;
            case linkText:
                log.debug("find element by linkText");
                e = driver.findElement(By.linkText(locator.getExpression()));
                break;
            case partialLinkText:
                log.debug("find element by partialLinkText");
                e = driver.findElement(By.partialLinkText(locator.getExpression()));
                break;
            case tagName:
                log.debug("find element by tagName");
                e = driver.findElement(By.tagName(locator.getExpression()));
                break;
            case className:
                log.debug("find element by className");
                e = driver.findElement(By.className(locator.getExpression()));
                break;
            case cssSelector:
                log.debug("find element by cssSelector");
                e = driver.findElement(By.cssSelector(locator.getExpression()));
                break;
            default:
                e = driver.findElement(By.xpath(locator.getExpression()));
        }
        return  e;
    }

    private Locator getLocator(String locatorName) {
        Locator locator = null;
        log.info("从locatorMap里取出"+locatorName+"对应的locator");
        if (locatorMap!=null){
            locator = locatorMap.get(locatorName);
        }
        return locator;
    }

    public Locator locator(String locatorName){
        Locator locator = new Locator(locatorName);
       if (locatorMap!=null){
           locator = locatorMap.get(locatorName);
       }
       return locator;
    }
    public WebDriver getDriver(){
        return driver;
    }
}
